#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <string>
using namespace std;


int main(int argc, char const *argv[])
{
	string fileName = "../../images/Lenna.png";
	cv::Mat img = cv::imread(fileName, 1);
	cv::Mat img2 = cv::imread(fileName, 0);
	cout << "灰度通道数:" << img2.channels() << endl;
	if(img.empty())
		return -1;
	cv::namedWindow("test", cv::WINDOW_AUTOSIZE);
	cv::imshow("test", img);
	cv::imshow("test2", img2);
	

	cout<<"width:"<<img.rows<<"\t"<<"height:"<<img.cols<<endl;

	cout << "type:" << img.type() << endl;

	//cv::Mat grayImage_1 = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);	//仅取蓝色B
	auto grayImage_1 = cv::Mat(img.rows, img.cols, CV_8UC1);
	cv::Mat grayImage_2 = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);	//仅取绿色G
	cv::Mat grayImage_3 = cv::Mat::zeros(img.rows, img.cols, CV_32FC1);	//仅取红色R
	cv::Mat grayImage_4 = cv::Mat::zeros(img.rows, img.cols, CV_32FC1);	//Gray=R*0.3+G*0.59+B*0.11
	cv::Mat grayImage_5 = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);	//Gray=(R*30+G*59+B*11)/100
	cv::Mat grayImage_6 = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);  //Gray =(R*76+G*151+B*28)>>8;
	cv::Mat grayImage_7 = cv::Mat::zeros(img.rows, img.cols, CV_32FC1);  //Gray=（R+G+B）/3;

	
	for (int i = 0; i < img.rows; i++) {
		for (int j = 0; j < img.cols; j++) {
			grayImage_1.at<uchar>(i, j) = img.at<cv::Vec3b>(i, j)[0];
			grayImage_2.at<uchar>(i, j) = img.at<cv::Vec3b>(i, j)[1];
			grayImage_3.at<float>(i, j) = img.at<cv::Vec3b>(i, j)[2];
			grayImage_4.at<float>(i, j) = img.at<cv::Vec3b>(i, j)[0]*0.11+img.at<cv::Vec3b>(i, j)[1]*0.59+
										  img.at<cv::Vec3b>(i, j)[2]*0.3;
			grayImage_5.at<uchar>(i, j) = (img.at<cv::Vec3b>(i, j)[0]*11+img.at<cv::Vec3b>(i, j)[1]*59+
										  img.at<cv::Vec3b>(i, j)[2]*30)/100;
			grayImage_6.at<uchar>(i, j) = (img.at<cv::Vec3b>(i, j)[0]*28+img.at<cv::Vec3b>(i, j)[1]*151+
										  img.at<cv::Vec3b>(i, j)[2]*76)>>8;
			grayImage_7.at<float>(i, j) = (img.at<cv::Vec3b>(i, j)[0]+img.at<cv::Vec3b>(i, j)[1]+
										  img.at<cv::Vec3b>(i, j)[2])/3;
	
		}
	}

	auto oneMat = cv::Mat::ones(img.rows, img.cols, CV_32FC1);

	cout << oneMat.size() << "\t" << grayImage_7.size() << endl;

	//cv::log(grayImage_3+oneMat, grayImage_3);
	cv::Mat grayImage_8 = grayImage_3.clone();
	//cv::Mat grayImage_8(grayImage_3);
	cv::pow(grayImage_8/255, 2, grayImage_8);
	grayImage_8 = grayImage_8 * 255;

	cv::imwrite("../../images/gray_3_1.jpg", grayImage_8);

	cv::imwrite("../../images/gray.jpg", img2);
	cv::imwrite("../../images/gray_1.jpg", grayImage_1);
	cv::imwrite("../../images/gray_2.jpg", grayImage_2);
	cv::imwrite("../../images/gray_3.jpg", grayImage_3);
	cv::imwrite("../../images/gray_4.jpg", grayImage_4);
	cv::imwrite("../../images/gray_5.jpg", grayImage_5);
	cv::imwrite("../../images/gray_6.jpg", grayImage_6);
	cv::imwrite("../../images/gray_7.jpg", grayImage_7);
	//cv::imshow("test3", grayImage);

	cv::waitKey(-100);


	cv::destroyWindow("test");
	return 0;
}